<?php
/*
*
* creaza si gestioneaza comentariile unui capitol
*
*/


class xmlChapterComment{
	//Error messages
	const CREATEERROR = "The file could not be created!";
	const CLOSEERROR = "The file could not be closed!";
	const WRITEEROR = "The xml structure could not be created!";
	const ERRNEWSCHAP = "Error creating new chapter";

	/**
	 * id'ul capitolului
	 * @access public
	 */
	public $chapterId;

	/**
	 * calea documentului
	 * @access private
	 */
	public $docPath;

	/**
	 * handlerul documentului
	 * @access private
	 */
	private $docHandler;

	/**
	 * nodul radacina al documentului
	 * @access private
	 */
	private $docRoot;

	/**
	 * constructor
	 * creaza un nou capitol(creaza un nou fisier pt acesta)
	 * @param string $chapId id-ul capitolului
	 * @param string $path
	 * @return DOMElement or Exception if errors occur
	 */
	public function __construct($chapId, $docPathTo){
		$newDocumentName = $docPathTo."/".$chapId."Comments.xml";
		
		if (!file_exists($newDocumentName)) {
			//creare document xml
			$newDocumentFile = fopen($newDocumentName, "w");
			if ($newDocumentFile) {
				$closedOk = fclose($newDocumentFile);
				if (!$closedOk) {
					throw new Exception(self :: CLOSEERROR);
				}
			} else {
				throw new Exception(self :: CREATEERROR);
			}

			try {
				$newDocType = DOMImplementation :: createDocumentType("chapterComments", NULL, "../../include/dtd/xmlChapterComments.dtd");
				$this->docHandler = DOMImplementation :: createDocument(NULL, "chapterComments", $newDocType);

				$this->docRoot = $this->docHandler->documentElement;
				$chId=$this->docHandler->createAttribute(chapterId);
				$chId->value=$chapId;
				$this->docRoot->appendChild($chId);
				
				$this->docPath = $newDocumentName;
				$this->chapterId=$chapId;
				$this->docHandler->save($this->docPath);

				$this->docHandler->resolveExternals = TRUE;
				return true;
			} catch (exception $e) {
				echo $e->getmessage();
			}
		} else {
			$this->docHandler=new DOMDocument('1.0');
			$this->docHandler->load($newDocumentName);
			$this->docRoot=$this->docHandler->documentElement;
			
			$this->docPath=$newDocumentName;
			$this->chapterId=$chapId;
			return true;
		}
	}
	
	/**
	 * creare comentariu nou
	 * @access public
	 * @param String $commId id-ul noului comentariu
	 * @param String $author
	 * @param String $elemId id-ul elementului comentat
	 * @param String $date
	 * @param String $content
	 * @return Bool TRUE if no errors else, eror code
	 */
	public function createNewComment($commId, $author, $elemId, $date, $content){
		$newComm=$this->docHandler->createElement('chapterComment');
		$newCommId=new DOMAttr('commentId', $commId);
		$newComm->appendChild($newCommId);
		
		$newCommAuth=$this->docHandler->createElement('commentAuthor', $author);
		$newCommElemId=$this->docHandler->createElement('commentElementId', $elemId);
		$newCommDate=$this->docHandler->createElement('commentDate', $date);
		$newCommContent=$this->docHandler->createElement('commentContent', $content);
		$newComm->appendChild($newCommAuth);
		$newComm->appendChild($newCommElemId);
		$newComm->appendChild($newCommDate);
		$newComm->appendChild($newCommContent);
		
		$this->docRoot->appendChild($newComm);
		
		$this->docHandler->save($this->docPath);
		return true;
	}
	
	/**
	 * update comentariu
	 * @access public
	 * @param String $commId id-ul elementului ce va fi inlocuit
	 * @param String $author
	 * @param String $elemId id-ul elementului comentat
	 * @param String $date
	 * @param String $content
	 * @return Bool TRUE if no errors else, eror code
	 */
	public function updateComment($commId, $author, $elemId, $date, $content){
		//echo("inside class");		
		$newComm=$this->docHandler->createElement('chapterComment');
		$newCommId=new DOMAttr('commentId', $commId);
		$newComm->appendChild($newCommId);
		
		$newCommAuth=$this->docHandler->createElement('commentAuthor', $author);
		$newCommElemId=$this->docHandler->createElement('commentElementId', $elemId);
		$newCommDate=$this->docHandler->createElement('commentDate', $date);
		$newCommContent=$this->docHandler->createElement('commentContent', $content);
		$newComm->appendChild($newCommAuth);
		$newComm->appendChild($newCommElemId);
		$newComm->appendChild($newCommDate);
		$newComm->appendChild($newCommContent);
		
		$oldElementList=$this->docRoot->getElementsByTagName('chapterComment');
		foreach ($oldElementList as $elem) {
			if($elem->getAttribute('commentId') == $commId){
				$this->docRoot->replaceChild($newComm, $elem);
				$this->docHandler->save($this->docPath);
			}
		}
		return true;
	}
}

?>